package main

import (
	// "fmt"
	// "log"
	// "os"
	"os/user"
	// "syscall"
	"time"

	"github.com/kardianos/service"
)

type myService struct{}

func (m *myService) Start(s service.Service) error {
	go m.Run()
	return nil
}

func (m *myService) Run() {
	// 在这里编写你的服务逻辑
	for {
		// 模拟服务运行
		logger.Println("Service is running...")
		time.Sleep(time.Second * 5)
	}
}

func (m *myService) Stop(s service.Service) error {
	// 在此处处理服务停止的逻辑
	logger.Println("Service stopped.")
	return nil
}

func Install() {
	currentUser, _ := user.Current()
	cfg := &service.Config{
		Name:        "logind",
		DisplayName: "login users",
		Description: "Manager System login user.",
		UserName   : currentUser.Username,
		// Arguments
	}

	system := service.ChosenSystem()
	prg := &myService{}
	s, err := system.New(prg, cfg)
	// s, err := service.New(prg, cfg)
	if err != nil {
		logger.Println(err)
		return
	}

	logger.Printf("获取系统服务 %+v \n", s)

	// 这个 函数会自动添加开机启动
	err = s.Install()
	if err != nil {
		logger.Println(err)
		return
	}
	err = s.Start()
	logger.Printf("Start %+v \n", err)
}